package com.lqr.javabean;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;

//@WebListener
public class UserListener implements ServletContextListener {

    public UserListener() {
    }

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("服务器启动 创建spring容器对象");
        //读取配置文件创建spring容器对象
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        //存入servletContext作用域中
        ServletContext servletContext = sce.getServletContext();
        servletContext.setAttribute("spring",applicationContext);

    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("服务器关闭 销毁spring容器对象");
        ServletContext servletContext = sce.getServletContext();
        ClassPathXmlApplicationContext applicationContext= (ClassPathXmlApplicationContext) servletContext.getAttribute("spring");
        //关闭spring容器对象
        applicationContext.close();
    }

}